<template>
  <div>
    <search-condition @search="onSearch" @reset="onReset" :showOther="false"/>
    <el-card>
      <div class="clearfix" style="margin-bottom:16px" v-show="publicElectiveStandard && publicElectiveStandard.length">
        <div class="edu-page-title mg-bottom">
          <span>{{$t("auditStandard.creditStandard")}}</span>
        </div>
        <!-- 通识选修课标准 -->
        <public-elective-standard :isSetting="true" ref="elective"/>
        <div class="float-right" style="margin-top: 16px;">
          <el-button @click="onCancel">{{$t("message.cancel")}}</el-button>
          <el-button @click="onSubmit" type="primary">{{$t("message.confirm")}}</el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>
<script>
  import api from 'common/src/api/preparatoryGraduation/auditStandards';
  export default {
    name: 'setPublicElectiveStandardTab',
    components: {
      searchCondition: () => import('./searchCondition'),
      publicElectiveStandard: () => import('./publicElectiveStandard')
    },
    data() {
      return {
        publicElectiveStandard: [],
        queryForm: {}
      };
    },
    methods: {
      onSearch(condition) {
        api.query(condition).then(res => {
          this.queryForm = condition;
          const data = res && res.data;
          this.publicElectiveStandard = data.publicElectiveStandard;
          if (!this.publicElectiveStandard.length) {
            this.publicElectiveStandard.push({
              credit: 0
            });
          }
          this.initTableContent();
        });
      },
      onReset() {
        this.publicElectiveStandard = [];
        this.initTableContent();
      },
      initTableContent() {
        this.$refs['elective'].init(this.publicElectiveStandard);
      },
      onCancel() {
        this.onSearch(this.queryForm);
      },
      onSubmit() {
        const data = this.$refs['elective'].standardTableData;
        if (data[0].credit < 3) {
          this.$message({
            type: 'error',
            message: '通识选修课学分标准至少为3学分'
          });
          return;
        }
        api.generalSetting({
          ...this.queryForm,
          list: [{
            credit: data[0].credit,
            labName: '通识选修课'
          }]
        }).then(() => {
          this.$message({
            type: 'success',
            message: '操作成功！'
          });
        });
      }
    }
  };
</script>
<style lang="scss" scoped>
  .mg-bottom{
    margin-bottom: 15px;
  }
  .edu-page-title {
    span {
      line-height: 24px;
      display: inline-block;
      padding-right: 10px;
      font-size: 18px;
      font-weight: bold;
      color: #2b3b4e;
    }
  }
</style>
